package com.ibm.spatiotemp.test.requestServer;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.config.LocalConfig;
import com.ibm.spatiotemp.test.requestServer.redis.RedisPubSub;
import com.ibm.spatiotemp.utils.StringEncoder;


public class JedisPubSubResponseSender extends ResponseSender{
	
	public static Logger logger = Logger.getLogger(JedisPubSubResponseSender.class);
	
	public RedisPubSub puber;
	
	public JedisPubSubResponseSender(String servip, String port){
		puber = new RedisPubSub();
		puber.init(servip, port);
	}

	@Override
	public void sendResonse(String topic, String response, boolean compress) {
//		JsonObject jsonobj = new JsonObject(response);
//		String topic = jsonobj.getAttributeAsString(JsonObject.ResponseTags[0]);
//		String response_content = jsonobj.getAttributeAsString(JsonObject.ResponseTags[1]);
		if(compress){
			String compressed = null;
		try {
			compressed = StringEncoder.compress(LocalConfig.codecClassname, response);
			logger.info("after compression length: "+ compressed.length());
		} catch (ClassNotFoundException | IOException e) {
			puber.publish(topic, "error in compression");
			return;
		}
		puber.publish(topic, compressed);
		}
	else
		puber.publish(topic, response);
	}

}
